﻿using NBpn.Exception;
using NBpn.Network;
using Xunit;

namespace NBpn.Test.Network
{
    public class NeuronFixture
    {
        [Fact]
        public void IsInputNeuron()
        {
            LinealNeuron n = ObjectMother.GetInputLinealNeuron(2.0);
            Assert.True(n.IsInputNeuron);
        }

        [Fact]
        public void ConnectNeurons()
        {
            //  2.0      1.5
            // ----> n1 ----> n2

            InputLinealNeuron n1 = ObjectMother.GetInputLinealNeuron(2.0);
            SigmoidalNeuron n2 = ObjectMother.GetSigmoidalNeuron();

            n1.ConnectWith(n2, 1.5);

            Assert.Equal(1, n1.InputConnections.Count);
            Assert.Equal(1, n1.ForwardConnections.Count);

            Assert.Equal(1, n2.InputConnections.Count);
            Assert.Equal(0, n2.ForwardConnections.Count);

            Assert.Equal(1, n1.Net.Connections.Count);

            Assert.Equal(2.0, n1.Net.Value);
            Assert.Equal(3.0, n2.Net.Value);
        }


        [Fact]
        public void NetOfNeurons()
        {
            InputLinealNeuron in1 = ObjectMother.GetInputLinealNeuron(2.0);
            InputLinealNeuron in2 = ObjectMother.GetInputLinealNeuron(2.0);
            InputLinealNeuron in3 = ObjectMother.GetInputLinealNeuron(2.0);

            Neuron n = ObjectMother.GetSigmoidalNeuron();

            in1.ConnectWith(n, 1.0);
            in2.ConnectWith(n, 1.5);
            in3.ConnectWith(n, 2.0);

            Assert.Equal(9.0, n.Net.Value);
        }

        [Fact]
        public void NeuronNotConnected()
        {
            INeuron n = new SigmoidalNeuron();

            Assert.Throws<BpnException>(
                delegate
                {
                    double foo = n.Net.Value;
                });
        }


    }
}